package jdbcTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Add {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection conn;
        Statement stmt;
        //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
        conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        //3.定义SQL
        String sql="insert into student values(200813145,'张三','男',52,'av')";
        //4.获取SQL对象sattament
            stmt=conn.createStatement();

          //开启事务
         conn.setAutoCommit(false);
        try {
            //开启事务

            //5.执行SQL
            int i=  stmt.executeUpdate(sql);
            System.out.println(i);
            if(i>0) {
                System.out.println("插入成功");
                //提交事务
                conn.commit();
            }
            else {

                System.out.println("插入失败");
            }
        } catch (Exception e) {
            //回滚事务
            conn.rollback();
            e.printStackTrace();
        }

        //释放资源
            stmt.close();
            conn.close();
        }



}


